package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.StockDao;
import cn.gson.crm.model.dao.StockDetailsDao;
import cn.gson.crm.model.mapper.StockaMapper;
import cn.gson.crm.model.pojos.ReturnGoods;
import cn.gson.crm.model.pojos.Stock;
import cn.gson.crm.model.pojos.StockDetails;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class StocksService {
    @Autowired
    StockaMapper stockaMapper;
    @Autowired
    StockDao stockDao;
    @Autowired
    StockDetailsDao stockDetailsDao;
    //新增
    public void savestocl(Stock stock){
        boolean fasl=false;
        //查询产品是否存在
        List<StockDetails> stockDetails2 = null;
        for(int  i=0;i<stock.getStockDetailsList().size();i++){
            System.out.println("进来------------------");
         List<StockDetails> stockDetails=stockaMapper.selectkucun(stock.getStockDetailsList().get(i).getsColor(),stock.getStockDetailsList().get(i).getsCompany(),stock.getStockDetailsList().get(i).getsSize(),stock.getStockDetailsList().get(i).getsName());
         //如果集合有值就修改数量，否则新增
            String name=stock.getStockDetailsList().get(i).getsName()+"";
            stockDetails2=stockDetailsDao.selectstokdel(name);
            System.err.println("stockDetails2长度："+stockDetails2.size());
          if(stockDetails.isEmpty()){
              //查询此产品名是否存在,存在新增从表否则新增主从表
              System.err.println("------------=="+stockDetails2+"---"+stock.getStockDetailsList().get(i).getsName());
              if(stockDetails2.size()>0 &&!stockDetails2.isEmpty()){
                  System.err.println("存在---------------");
                  stock.getStockDetailsList().get(i).setStock(stockDetails2.get(0).getStock());
                  stockDetailsDao.save(stock.getStockDetailsList().get(i));
              }else{
                  System.err.println("不存在---------------");
                  Stock aa=new Stock();
                  aa.setKcId(0);
                  aa.setCpName(stock.getStockDetailsList().get(i).getsName());
                  aa.setKcName("主仓库");
                  stockDao.save(aa);
                  stock.getStockDetailsList().get(i).setStock(aa);
                  stockDetailsDao.save(stock.getStockDetailsList().get(i));
              }
          }else{
              System.out.println("修改---------------");
              stockaMapper.updatekucun(stockDetails.get(0).getsNumber()+stock.getStockDetailsList().get(i).getsNumber(),stockDetails.get(0).getStdeltId());
          }
        }
    }
    //库存查询所有库存记录
    public Map<String,Object> selectkucunsy(Integer page, Integer size){
        List<StockDetails> stockDetails2=stockaMapper.selectkucunsy();
        Page<Object> obj = PageHelper.startPage(page, size);
        List<StockDetails> stockDetails=stockaMapper.selectkucunsy();
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",stockDetails);
        map.put("tong",stockDetails2);
        return map;
    };
    public Map<String,Object>  selectkucunsywhere(String where,Integer page, Integer size){
        Page<Object> obj = PageHelper.startPage(page, size);
        List<StockDetails> stockDetails=stockaMapper.selectkucunsywhere(where);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",stockDetails);
        return map;
    };
}
